package com.example.stuwork.service.impl;

import com.example.stuwork.common.Result;
import com.example.stuwork.dao.QuizQuestionDao;
import com.example.stuwork.pojo.QuizQuestion;
import com.example.stuwork.service.QuizQuestionService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 测试题目服务实现类
 */
@Service
public class QuizQuestionServiceImpl implements QuizQuestionService {

    @Autowired
    private QuizQuestionDao quizQuestionDao;
    
    @Autowired
    private ObjectMapper objectMapper;

    @Override
    public Result<List<QuizQuestion>> getQuestionsByQuizId(Long quizId) {
        try {
            List<QuizQuestion> questions = quizQuestionDao.findByQuizId(quizId);
            
            // 解析选项字符串为选项列表
            for (QuizQuestion question : questions) {
                try {
                    List<String> optionList = objectMapper.readValue(question.getOptions(), List.class);
                    question.setOptionList(optionList);
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                }
            }
            
            return Result.success(questions);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取测试题目列表失败");
        }
    }

    @Override
    public Result<QuizQuestion> getQuestionById(Long questionId) {
        try {
            QuizQuestion question = quizQuestionDao.getById(questionId);
            if (question == null) {
                return Result.error("题目不存在");
            }
            
            // 解析选项字符串为选项列表
            try {
                List<String> optionList = objectMapper.readValue(question.getOptions(), List.class);
                question.setOptionList(optionList);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
            
            return Result.success(question);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取题目信息失败");
        }
    }

    @Override
    public Result<?> addQuestion(QuizQuestion question) {
        try {
            // 将选项列表转换为JSON字符串
            if (question.getOptionList() != null && !question.getOptionList().isEmpty()) {
                String optionsJson = objectMapper.writeValueAsString(question.getOptionList());
                question.setOptions(optionsJson);
            }
            
            int result = quizQuestionDao.insert(question);
            if (result > 0) {
                return Result.success("添加题目成功");
            } else {
                return Result.error("添加题目失败");
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return Result.error("选项数据格式错误");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("添加题目失败");
        }
    }

    @Override
    public Result<?> batchAddQuestions(List<QuizQuestion> questions) {
        try {
            // 处理每个题目的选项列表
            for (QuizQuestion question : questions) {
                if (question.getOptionList() != null && !question.getOptionList().isEmpty()) {
                    String optionsJson = objectMapper.writeValueAsString(question.getOptionList());
                    question.setOptions(optionsJson);
                }
            }
            
            int result = quizQuestionDao.batchInsert(questions);
            if (result > 0) {
                return Result.success("批量添加题目成功");
            } else {
                return Result.error("批量添加题目失败");
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return Result.error("选项数据格式错误");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("批量添加题目失败");
        }
    }

    @Override
    public Result<?> updateQuestion(QuizQuestion question) {
        try {
            // 将选项列表转换为JSON字符串
            if (question.getOptionList() != null && !question.getOptionList().isEmpty()) {
                String optionsJson = objectMapper.writeValueAsString(question.getOptionList());
                question.setOptions(optionsJson);
            }
            
            int result = quizQuestionDao.update(question);
            if (result > 0) {
                return Result.success("更新题目成功");
            } else {
                return Result.error("更新题目失败");
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return Result.error("选项数据格式错误");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("更新题目失败");
        }
    }

    @Override
    public Result<?> deleteQuestion(Long questionId) {
        try {
            int result = quizQuestionDao.deleteById(questionId);
            if (result > 0) {
                return Result.success("删除题目成功");
            } else {
                return Result.error("删除题目失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除题目失败");
        }
    }

    @Override
    public Result<?> deleteQuestionsByQuizId(Long quizId) {
        try {
            int result = quizQuestionDao.deleteByQuizId(quizId);
            return Result.success("删除测试题目成功，共删除" + result + "条记录");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除测试题目失败");
        }
    }
} 